package com.easepal802s.project.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.clj.fastble.data.BleDevice;
import com.easepal802s.project.utils.ActivityUtils;
import com.ogawa.base.Constant.BleConstant;
import com.easepal802s.project.R;

import com.easepal802s.project.observer.BleDataObserver;
import com.easepal802s.project.ui.presenter.AttitudeAdjustmentPressenter;
import com.easepal802s.project.ui.iview.IAttitudeAdjustmentView;
import com.easepal802s.project.ble.BleController;
import com.easepal802s.project.widget.LayoutNormalTitle;
import com.ogawa.base.utils.CommonUtil;

import java.util.ArrayList;
import java.util.List;

import static com.easepal802s.project.ble.BleController.CONNECT_BREAK;


/**
 * 姿态调节
 */
public class AttitudeAdjustmentActivity extends BaseMvpTestActivity<AttitudeAdjustmentPressenter, AttitudeAdjustmentActivity> implements View.OnClickListener ,IAttitudeAdjustmentView, BleDataObserver.OnBleListerner {
    private LayoutNormalTitle mLayoutTitle;
    private LinearLayout mLlReset;
    private LinearLayout mLlZero1;
    private LinearLayout mLlZero2;
    private ImageView mIvReset;
    private ImageView mIvZero1;
    private ImageView mIvZero2;
    private List<TextView> mIvAdjustViews = new ArrayList<>();
    @Override
    public AttitudeAdjustmentPressenter initPresenter() {
        return new AttitudeAdjustmentPressenter(this);
    }

    @Override
    protected void initCreateView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_attitude_adjustment);
        BleDataObserver.getInst().registerBleDataObserver(this);
        initView();
    }

    @Override
    public AttitudeAdjustmentActivity initActivity() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BleDataObserver.getInst().removeBleDataObserver(this);
        mLayoutTitle.onDestory();
    }




    private void initView() {
        mLayoutTitle = findViewById(R.id.activity_adjust_title);
        mLayoutTitle.setGreenBackBotton();
        mLayoutTitle.setTitle("姿态调节");

        TextView chairup         =  findViewById(R.id.chairup);
        TextView chairdown       =  findViewById(R.id.chairdown);
        TextView chair_hand_up   =  findViewById(R.id.chair_hand_up);
        TextView cahir_leg_down  =  findViewById(R.id.cahir_leg_down);
        TextView chair_foot_down =  findViewById(R.id.chair_foot_down);
        TextView chair_foot_up   =  findViewById(R.id.chair_foot_up);
        mIvAdjustViews.add(chair_foot_up);
        mIvAdjustViews.add(chair_foot_down);
        mIvAdjustViews.add(cahir_leg_down);
        mIvAdjustViews.add(chair_hand_up);
        mIvAdjustViews.add(chairdown);
        mIvAdjustViews.add(chairup);
        mIvReset= findViewById(R.id.iv_reset);
        mIvZero1= findViewById(R.id.iv_zero1);
        mIvZero2= findViewById(R.id.iv_zero2);
        mLlReset = findViewById(R.id.ll_reset);
        mLlZero1 = findViewById(R.id.ll_zero1);
        mLlZero2 = findViewById(R.id.ll_zero2);
        mLlReset.setOnClickListener(this);
        mLlZero1.setOnClickListener(this);
        mLlZero2.setOnClickListener(this);
        chairup.setOnClickListener(this);
        chairdown.setOnClickListener(this);
        chair_hand_up.setOnClickListener(this);
        cahir_leg_down.setOnClickListener(this);
        chair_foot_down.setOnClickListener(this);
        chair_foot_up.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.chairup:
                sendCommond(BleConstant.SHENBEI);
                break;
            case R.id.chairdown:
                sendCommond(BleConstant.DAOBEI);
                break;
            case R.id.chair_hand_up:
                sendCommond(BleConstant.SUOTUI);
                break;
            case R.id.chair_foot_down:
                sendCommond(BleConstant.JIANGTUI);
                break;
            case R.id.chair_foot_up:
                sendCommond(BleConstant.TAITUI);
                break;
            case R.id.cahir_leg_down:
                sendCommond(BleConstant.SHENTUI);
                break;
            case R.id.ll_reset:
                sendCommond(BleConstant.FUWEI);
                break;
            case R.id.ll_zero1:
                sendCommond(BleConstant.LINZL1);
                break;
            case R.id.ll_zero2:
                sendCommond(BleConstant.LINZL2);
                break;


        }
    }

    private void sendCommond(final String commond){
        BleController.getInst().sendSingleData(commond, true);
    }


    @Override
    public void getData(String data) {
        if(data.equals(AttitudeAdjustmentPressenter.IS_RESET)){
            mIvReset.setSelected(true);
            mIvZero1.setSelected(false);
            mIvZero2.setSelected(false);
        }
       else if(data.equals(AttitudeAdjustmentPressenter.IS_ZERO1)){
            mIvReset.setSelected(false);
            mIvZero1.setSelected(true);
            mIvZero2.setSelected(false);
        }
      else   if(data.equals(AttitudeAdjustmentPressenter.IS_ZERO2)){
            mIvReset.setSelected(false);
            mIvZero1.setSelected(false);
            mIvZero2.setSelected(true);
        }
        else {
            mIvReset.setSelected(false);
            mIvZero1.setSelected(false);
            mIvZero2.setSelected(false);
        }

    }

    @Override
    public void adjustData(int flag) {
        for(int i = 0;i<mIvAdjustViews.size();i++){
            if(i==flag){
                mIvAdjustViews.get(i).setSelected(true);
            }
            else {
                mIvAdjustViews.get(i).setSelected(false);
            }
        }
    }


    @Override
    public void onConnectState(int state) {

    }

    @Override
    public void onTargetDeviceFind(BleDevice bleDevice) {

    }

    @Override
    public void onStarScan() {

    }

    @Override
    public void onNoDeviceFind() {

    }

    @Override
    public void onGetData(String data) {
        mBasePresenter.getDatas(data);
    }

    @Override
    public void onPowerOff() {
        if(BleController.getInst().getConnectState()==CONNECT_BREAK){
            ActivityUtils.startHomeActivity(this,"BREAK");
        }
        else {
            ActivityUtils.startHomeActivity(this,"none");
        }
        this.finish();

    }

    @Override
    public void onDataChange(String bage, int auto, int skill, int air,String name,String commond) {

    }

    @Override
    public void onShoulderAdjust(int data) {
        if(CommonUtil.isActivityTop(this.getClass(),this)&&data==1){
            ActivityUtils.startShouldersAdjustmentActivity(this);
        }
    }
}
